<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="学号" prop="projectId">
        <el-input
          v-model="queryParams.userNumber"
          placeholder="请输入学号"
          clearable
          size="small"
          prefix-icon="el-icon-search"
          style="margin-bottom: 20px"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['business:user:list']"
        >导入
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" :search="false" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column label="班级" align="center" prop="className"  />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="学号" align="center" prop="userNumber" />
      <el-table-column label="角色" align="center" prop="role" />
      <el-table-column label="课程" align="center" prop="courseProject"  />
      <el-table-column label="起始时间" align="center" prop="startTime" />
      <el-table-column label="截止时间" align="center" prop="endTime" />
      <el-table-column label="加入时间" align="center" prop="joinTime" />
      <el-table-column label="退出时间" align="center" prop="outTime" />
      <el-table-column label="综合评语" align="center" prop="comment" />
      <el-table-column label="获得荣誉" align="center" prop="honour" />
      <el-table-column label="状态" align="center" prop="status"/>
      <el-table-column label="备注1" align="center" prop="note1"/>
      <el-table-column label="备注2" align="center" prop="note2"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="综合评语" prop="name">
          <el-input v-model="form.comment" placeholder="请输入综合评语" />
        </el-form-item>
        <el-form-item label="获得荣誉(使用,分割)" prop="appId">
          <el-input v-model="form.honour" placeholder="请输入获得荣誉" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="addOpen" width="600px" append-to-body>
      <el-form ref="form2" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="班级" prop="className">
          <el-input v-model="form.className" placeholder="请输入班级" />
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="学号" prop="userNumber">
          <el-input v-model="form.userNumber" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="form.role" placeholder="请输入角色" />
        </el-form-item>
        <el-form-item label="课程"  prop="courseProject">
          <el-input v-model="form.courseProject" placeholder="请输入课程" />
        </el-form-item>
        <el-form-item label="起始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="date"
            placeholder="选择开始时间"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止时间"  prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="date"
            placeholder="选择截止时间"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="加入时间" prop="joinTime">
          <el-date-picker
            v-model="form.joinTime"
            type="date"
            placeholder="选择加入时间"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退出时间"  prop="outTime">
          <el-date-picker
            v-model="form.outTime"
            type="date"
            placeholder="选择退出时间"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="综合评语"  >
          <el-input v-model="form.comment" placeholder="请输入综合评语" />
        </el-form-item>
        <el-form-item label="获得荣誉(使用,分割)"  >
          <el-input v-model="form.honour" placeholder="请输入获得荣誉" />
        </el-form-item>
        <el-form-item label="状态"  >
          <el-input v-model="form.status" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="备注1"  >
          <el-input v-model="form.note1" placeholder="请输入备注1" />
        </el-form-item>
        <el-form-item label="备注2"  >
          <el-input v-model="form.note2" placeholder="请输入备注2" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="addOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?actId=' + upload.actId"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  addUserApi,
  appSetCommentApi,
  appUserListApi
} from "@/api/business/gzh";
import {getToken} from "@/utils/auth";
import {appUpdateApi} from "@/api/business/app";

export default {
  name: "Post",
  dicts: ['sys_normal_disable', "sys_yes_no"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      addOpen: false,
      form: {},
      // 表单校验
      rules: {
        className: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        userNumber: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        role: [
          { required: true, message: "角色不能为空", trigger: "blur" }
        ],
        courseProject: [
          { required: true, message: "课程不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "请选择起始时间", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "请选择截止时间", trigger: "blur" }
        ],
        joinTime: [
          { required: true, message: "请选择加入时间", trigger: "blur" }
        ],
        outTime: [
          { required: true, message: "请选择退出时间", trigger: "blur" }
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        status: undefined,
        userNumber: "",
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,

        actId: "",
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/business/gzh/user/importData"
      },

    }
  },
  created() {
    setTimeout(this.getList, 500);
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      appUserListApi(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.importResult = response.data;
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/business/gzh/user/exportData', {}, `教务信息_${new Date().getTime()}.xlsx`)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = JSON.parse(JSON.stringify(row)) ;
      this.open = true;
      this.title = "修改";
    },
    /** 提交按钮 */
    submitForm: function() {
        appSetCommentApi(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.addOpen = true;
      this.title = "添加";
      this.form = {
        "className": "",
        "userName": "",
        "role": "",
        "courseProject": "",
        "startTime": "",
        "endTime": "",
        "joinTime": "",
        "outTime": "",
        "userNumber": "",
        "comment": "",
        "honour": "",
        "status": "",
        "note1": "",
        "note2": ""
      };
    },
    /** 提交按钮 */
    submitForm2: function() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          addUserApi(this.form).then(response => {
            this.$modal.msgSuccess("添加成功");
            this.addOpen = false;
            this.getList();
          });
        }
      });
    },

  }
};
</script>
